import axios from 'axios'
import {ElMessage} from "element-plus";
import router from "../router/index.js";

const request=axios.create({
    baseURL:'http://localhost:8080' ,
    timeout: 600000
})

//request拦截器
request.interceptors.request.use(
    (config)=>{
        const loginUser=JSON.parse(localStorage.getItem('loginUser'))
        if(loginUser&&loginUser.token){
            config.headers.token=loginUser.token
        }
        console.log(config)
        return config
    },
    (error)=>{
        return Promise.reject(error)
    }
)


//response拦截器
request.interceptors.response.use(
    (response)=>{
        return response.data
    },
    (error)=>{
        if(error.response.status==401){
            console.log(error)
            ElMessage.error('登陆超时，请重新登录')
            router.push('/login')

        }else{
            ElMessage.error('接口访问异常')
        }

        return Promise.reject(error)
    }
)

export default request